package top.zhangjianyong.tools.service.mybatis.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.marirui.JJLSKX;
import top.zhangjianyong.tools.mapper.JJLSKXMapper;
import top.zhangjianyong.tools.service.mybatis.IJJLSKXService;
import java.util.List;

/**
 * @author zhangjianyong
 * @since 2025/4/15 08:31
 */
@Slf4j
@Service
public class IJJLSKXServiceImpl extends ServiceImpl<JJLSKXMapper, JJLSKX> implements IJJLSKXService {

    @Lazy
    @Autowired
    private IJJLSKXService ijjlskxService;

    @Override
    public void saveOrUpdate(List<JJLSKX> jjlskx, String code) {
        try {
            for (JJLSKX kx : jjlskx) {
                kx.setCode(code);
                JJLSKX one = lambdaQuery().eq(JJLSKX::getCode, code)
                        .eq(JJLSKX::getD, kx.getD())
                        .one();
                if (null == one) {
                    one = kx;
                } else {
                    kx.setId(one.getId());
                    one = kx;
                }
                ijjlskxService.saveOrUpdate(one);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public List<JJLSKX> listByCode(String code, Integer limit) {
        return lambdaQuery().eq(JJLSKX::getCode, code)
                .orderByDesc(JJLSKX::getD)
            .last(limit != null, "limit " + limit)
                .list();
    }
}
